package com.zxw.flow.controller.toc;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zxw.common.core.base.res.BaseRes;
import com.zxw.flow.domain.entity.toc.FlowJsonData;
import com.zxw.flow.domain.vo.toc.FlowJsonDataVO;
import com.zxw.flow.service.toc.FlowJsonDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-08-19
 */
@Api("流程图数据存储模块")
@RestController
@RequestMapping("/flow-json-data")
@RequiredArgsConstructor
public class FlowJsonDataController {

    private final FlowJsonDataService flowJsonDataService;

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("根据主键id查询流程图数据")
    @GetMapping("/{id}")
    public BaseRes<FlowJsonData> findFlowJsonDataById(@PathVariable("id") Long id) {
        return BaseRes.ok(flowJsonDataService.get(id));
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("根据文件树id查询流程图数据")
    @GetMapping("/file-path-id/{filePathId}")
    public BaseRes<FlowJsonData> findFlowJsonDataByFilePathId(@PathVariable("filePathId") Long filePathId) {
        FlowJsonData f= flowJsonDataService.getByFilePathId(filePathId);
        return BaseRes.ok(f);
    }


    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("保存||更新 流程图数据")
    @PostMapping("/insert-or-update")
    public BaseRes<FlowJsonDataVO> insertOrUpdate(@RequestBody FlowJsonDataVO flowJsonDataVO) {
        flowJsonDataService.insertOrUpdate(flowJsonDataVO);
        return BaseRes.okAdd();
    }


    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("添加流程图数据")
    @PostMapping("/insert")
    public BaseRes<FlowJsonDataVO> insertFlowJsonData(@RequestBody FlowJsonDataVO flowJsonDataVO) {
        flowJsonDataService.insert(flowJsonDataVO);
        return BaseRes.okAdd();
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("修改流程图数据")
    @PutMapping("/{id}")
    public BaseRes updateFlowJsonData(@PathVariable("id") Long id, @RequestBody FlowJsonDataVO flowJsonDataVO) {
        flowJsonDataService.update(flowJsonDataVO);
        return BaseRes.okUpdate();
    }


    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("删除流程图数据")
    @DeleteMapping("/{id}")
    public BaseRes deleteFlowJsonData(@PathVariable("id") Long id) {
        flowJsonDataService.delete(id);
        return BaseRes.okUpdate();
    }

}
